binaryDecode
Type
function
Summary
Decodes binary data and places it into the specified variables.
Syntax
binaryDecode(<formatsList>, <data>, <variablesList>)
Description
Use the binaryDecode function to convert binary data into a form that can be manipulated by handlers.
The binary data format used by binaryDecode is similar to the format produced by the "pack" function of the Perl programming language.
You must declare or otherwise create all variables in the variablesList before using them in the binaryDecode function. Unlike the put command, the binaryDecode function does not automatically create local variables when you use them.
Although the x dataType skips the specified number of bytes rather than converting them, you still must provide a variable for instances of x that appear in the formatsList. The binaryDecode function does not change the value of a variable used for the dataType x.
If the formatsList specifies fewer bytes than are in the data, the binaryDecode function ignores the remaining bytes. If the formatsList specifies more bytes than are in the data, the binaryDecode function converts as many of the dataTypes as there is data for. Check the value that the binaryDecode function returns to determine how much data was actually converted. Here is an example:
on convertStuff dataToConvert
global headerData,placeholder,bodyData,footerData
local convertResult
put binaryDecode(\"i5x2a24xi2\",dataToConvert, \
headerData,placeholder,bodyData,placeholder, \
footerData) into convertResult
if convertResult < 3 then return \"Error: data was corrupted\"
end convertStuff
Parameters
Name | Type | Description |
---|---|---|
formatsList | The formatsList consists of one or more dataTypes, each followed optionally by an amount. A dataType is one of the following single letters:
The amount corresponding to each dataType is an integer or the * character. If no amount is specified, the dataType is used for a single byte of data. The * character causes the rest of the data to be converted according to the formatType, so it should appear only as the last character in the formatsList.
| |
data | string | A string of encoded binary data. |
variablesList | A comma-separated list of local or global variable names. The number of variable names must be the same as the number of dataTypes specified in the formatsList, and the variables must already exist. |
Examples
binaryDecode("h","M",theHex) -- converts M to its hex equivalent
binaryDecode("a*",receivedData,textData) -- converts data to text
binaryDecode("x12Sh16",picHeader,junk,numColors,colorTable)
binaryDecode(myFormat,placeHolder,importantStuff)
Related
command: put
control structure: function
function: numToChar, format, value
glossary: return, binary file, variable, handler, local variable, byte, command
Compatibility and Support
Introduced
LiveCode 1.0
OS
mac
windows
linux
ios
android
Platforms
desktop
server
mobile